package org.dtrd.modules.achievement.entity.bean;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninTemplate;

import javax.validation.constraints.NotBlank;
import java.util.StringJoiner;

/**
 * <p>
 * 打卡计划模板
 * </p>
 *
 * @author QR
 * @since 2022-01-04
 */
@Getter
@Setter
@ApiModel(value = "打卡计划模板详情")
public class SigninTemplateContent {

    private Integer dataId;

    @ApiModelProperty("模板名称")
    private String signInName;

    @ApiModelProperty("周一计划")
    private Object[] signInJsonMonday;

    @ApiModelProperty("周二计划")
    private Object[] signInJsonTuesday;

    @ApiModelProperty("周三计划")
    private Object[] signInJsonWednesday;

    @ApiModelProperty("周四计划")
    private Object[] signInJsonThursday;

    @ApiModelProperty("周五计划")
    private Object[] signInJsonFriday;

    @ApiModelProperty("周六计划")
    private Object[] signInJsonSaturday;

    @ApiModelProperty("周日计划")
    private Object[] signInJsonSunday;

    @NotBlank
    @ApiModelProperty(value = "机构编码", required = true)
    private String orgCode;


    public SigninTemplateContent parseFromPo(DtrdEntRdSigninTemplate po) {
        dataId = po.getDataId();
        signInName = po.getSignInName();
        signInJsonMonday = addProperty(po.getSignInJsonMonday());
        signInJsonTuesday = addProperty(po.getSignInJsonTuesday());
        signInJsonWednesday = addProperty(po.getSignInJsonWednesday());
        signInJsonThursday = addProperty(po.getSignInJsonThursday());
        signInJsonFriday = addProperty(po.getSignInJsonFriday());
        signInJsonSaturday = addProperty(po.getSignInJsonSaturday());
        signInJsonSunday = addProperty(po.getSignInJsonSunday());
        orgCode = po.getOrgCode();
        return this;
    }

    private Object[] addProperty(String signinTemplate) {
        JSONArray jsonArray = new JSONArray(signinTemplate);
        JSONObject[] jsonObjects = new JSONObject[32];
        jsonObjects = jsonArray.toArray(jsonObjects);
        for (JSONObject jsonObject : jsonObjects) {
            Integer recordType = jsonObject.getInt("recordType");
            Integer subType = jsonObject.getInt("subType");
            jsonObject.set("item_id", recordType + "+" + subType);
            StringJoiner joiner = new StringJoiner("");
            if (subType != null) {
                if (recordType == Constant.RecordType.SUGAR.getType()) {
                    joiner.add(Constant.SugarRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.DIET.getType()) {
                    joiner.add(Constant.FoodRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.MEDICAL.getType()) {
                    joiner.add(Constant.MedicalRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.INSULIN.getType()) {
                    joiner.add(Constant.InsulinRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.BLOOD_PRESSURE.getType()) {
                    joiner.add(Constant.BloodPressureRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.NUTRIENT.getType()) {
                    joiner.add(Constant.NutrientRecordType.valueOf(subType).getName());
                }
            }
            if (recordType != null) {
                joiner.add(Constant.RecordType.valueOf(recordType).getName());
            }
            jsonObject.set("item", joiner.toString());
        }
        return jsonObjects;
    }
}
